package com.lyf.activemq.simple;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.component.jms.JmsProducer;
//import org.apache.activemq.broker.Connection;

import javax.jms.*;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/6
 * @VERSION 1.0
 * @DESC
 */
public class JmsProduceDemo {
    public static final String ACTIVE_URL = "tcp://47.98.99.88:61616";
    public static final String QUEUE_NAME = "queue-1";

    public static void main(String[] args) throws JMSException {
       // ActiveMQConnectionFactory'
         //创建连接工厂，并开启连接
         ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
         Connection connection =  activeMQConnectionFactory.createConnection();
         connection.start();

         // 开启session会话
         Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
         // 创建目的地（队列OR主题）
         Queue queue = session.createQueue(QUEUE_NAME);

         // JmsProducer producer = session.
        MessageProducer messageProducer = session.createProducer(queue);
        for (int i = 0; i < 3; i++) {
            // 创建消息
            TextMessage textMessage = session.createTextMessage();//"msg:"+i
            // 生产者发送
            messageProducer.send(textMessage);
        }

        // 关闭资源
        messageProducer.close();
        session.close();
        connection.close();
    }
}
